package board.dao;

import java.util.HashMap;

import org.apache.ibatis.session.SqlSession;

import board.vo.Member;

public class MemberDao {
	
	public Member selectOneMember(String id, String password) {
		
		Member member = null;
		
		SqlSession ss = MybatisConfig.getSqlSessionFactory().openSession();
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("id", id); map.put("password", password);
		
		try {
			member = ss.selectOne("member.selectOneMember_pw", map);
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(ss != null) ss.close();
		}
		
		System.out.println("MemberDao.selectOneMember(id, password)> member: " + member);
		return member;
		
	}//selectOneMember(id, password)
	
	public Member selectOneMember(String id) {
		
		Member member = null;
		
		SqlSession ss = MybatisConfig.getSqlSessionFactory().openSession();
		
		try {
			member = ss.selectOne("member.selectOneMember", id);
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(ss != null) ss.close();
		}
		
		System.out.println("MemberDao.selectOneMember(id)> member: " + member);
		return member;
		
	}//selectOneMember(id)

	public void updateOneMember(Member member) {
		
		SqlSession ss = MybatisConfig.getSqlSessionFactory().openSession();
		
		try {
			ss.update("member.updateOneMember", member);
			ss.commit();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(ss != null) ss.close();
		}
		
		System.out.println("MemberDao.updateOneMember(member)> member: " + member);
	}

	public int selectRights(String rights_value) {
		int rights_key = 0;
		
		SqlSession ss = MybatisConfig.getSqlSessionFactory().openSession();
		
		try {
			rights_key = ss.selectOne("member.selectRights", rights_value);
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(ss != null) ss.close();
		}
		
		System.out.println("MemberDao.selectRights()>" + rights_value + ": " + rights_key);
		
		return rights_key;
	}

	public void insertMember(String id, String name, String password, int rights_key) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("id", id); 
		map.put("name", name);
		map.put("password", password);
		map.put("rights_key", rights_key);
		
		SqlSession ss = MybatisConfig.getSqlSessionFactory().openSession();
		
		try {
			ss.insert("member.insertMember", map);
			ss.commit();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(ss != null) ss.close();
		}
		
		System.out.println("MemberDao.insertMember()> " + id + "/ " +name + "/ " +password + "/ " +rights_key);
		
	}

	public void deleteMember(String id) {
		
		SqlSession ss = MybatisConfig.getSqlSessionFactory().openSession();
		
		try {
			ss.insert("member.deleteMember", id);
			ss.commit();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(ss != null) ss.close();
		}
		
		System.out.println("MemberDao.deleteMember()> " + id);
		
	}
	
	
}//class
